<?php

require_once("../configuration.php");
require_once("global.php");
require_once("../includes/function.php");
require_once("../includes/product.php");
require_once("../includes/product.image.php");
require_once("../includes/category.php");
require_once("../includes/itemType.php");
require_once("../includes/product.php");
require_once("../includes/image.php");
// SHOW OFF: TEMPLATE
require_once("../includes/io.php");
require_once("../includes/template.php");
// required upload files
require_once("../includes/upload.php");
require_once("../includes/multiupload.php");
require_once("../includes/thumb.php");
require_once("../includes/uploadimage.php");

// VARIABLES
$products = "";
$category_options = "";
$itemType_options = ""; //AG
$product_demonstration = "";
$button_submit = "Create";
$product_rows = "";
$product_image_options = "";
$title = "Admin - Product - Edit";
$template = new template("../admin");
$defaulttemplate = new template("../default");
$product_form = new Product(); // reset product to its default values;
$image_form = new image();
$image_form->id = 0;
// ACTIONs
if (isset($_GET['import'])) {
    
} else
if (isset($_GET['delete'])) {
    $product = new Product();
    $product->id = (isset($_GET['delete'])) ? sql_injection($_GET['delete']) : 0;
    $product->delete();
    $mysql->c();
    header("location: news.php");
    exit();
} else
if (isset($_GET['edit'])) {
    // action for updating product values
    if (isset($_POST['create'])) {
        $product = new product();
        $product->id = (isset($_GET['edit'])) ? sql_injection($_GET['edit']) : NULL;
        $product->name = (isset($_POST['name'])) ? sql_injection($_POST['name']) : NULL;
        $product->image_link = (isset($_POST['image_link'])) ? sql_injection($_POST['image_link']) : NULL;
        $product->description = (isset($_POST['description'])) ? sql_injection($_POST['description']) : NULL;
        $product->modifiedDate= date('Y-m-d H:i');//AG
        $product->update_news();
        
        $mysql->c();
        header("location: news.action.php?edit={$product->id}#");
        exit();
    }

    $product_form = new Product();
    $product_form->id = (isset($_GET['edit'])) ? sql_injection($_GET['edit']) : 0;
    $product_form->getnews();
    //AG:Should be demo image
    //eval("\$product_demonstration = \"" . $template->get("news.demonstration.content") . "\";");

    $button_submit = "Edit";
} else
if (isset($_POST['create'])) {
    $image = new image();
    $image->link=(isset($_POST['image_link'])) ? sql_injection($_POST['image_link']) : NULL;
    $image->status = image_status::Active;
    $image->create();
    
    
    $product = new product();
    $product->id = (isset($_GET['edit'])) ? sql_injection($_GET['edit']) : NULL;
    $product->name = (isset($_POST['name'])) ? sql_injection($_POST['name']) : NULL;
    //$product->image_link = (isset($_POST['image_link'])) ? sql_injection($_POST['image_link']) : NULL;
    $product->image_id = $image->id;
    $product->description = (isset($_POST['description'])) ? sql_injection($_POST['description']) : NULL;
    $product->modifiedDate= date('Y-m-d H:i');//AG
    $product->create_news();

    $mysql->c();
    header("location: news.php?" . time());

    exit();
}

eval("\$menu_admin_rows = \"" . $template->get("menu.admin.rows") . "\";");
eval("\$header = \"" . $template->get("header") . "\";");
eval("\$menu = \"" . $template->get("menu") . "\";");
eval("\$sidebar = \"" . $template->get("sidebar") . "\";");
eval("\$content = \"" . $template->get("news") . "\";");
eval("\$footer = \"" . $template->get("footer") . "\";");
eval("\$index = \"" . $template->get("index") . "\";");

// display all
echo ($index); // can use echo $template->compress($index) to compress size of html

require_once("../configuration.end.php");
?>